﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace InstallApp
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            WriteLog(Environment.CommandLine);
     
            try
            {
                DoCommands(args[0].Replace("/", ""));
                WriteLog(args[0]);
            }
            catch (Exception ex)
            {
              WriteLog(ex.Message);

            }
        }

        private static void DoCommands(string arg)
        {
           
            string s = new System.IO.FileInfo(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath).DirectoryName ;
            WriteLog(s);
            string str = System.IO.File.ReadAllText( s+"\\KeelKit.AddIn", System.Text.Encoding.Unicode);
            str = str.Replace("KeelKit.dll", s + "\\KeelKit.dll");
            string vs2005 = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Visual Studio 2005\\Addins\\";
            string vs2008 = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Visual Studio 2008\\Addins\\";
            WriteLog( vs2005);
            WriteLog( vs2008);
            WriteLog("参数:"+arg );
            switch (arg)
            {
                case "installkeelkit":
                default:
                    WriteLog("安装:" + arg);
                    if (System.IO.Directory.Exists(vs2005))
                    {
                        WriteLog("2005:" + arg);
                        System.IO.File.WriteAllText(vs2005 + "KeelKit.AddIn", str, System.Text.Encoding.Unicode);
                    }
                    if (System.IO.Directory.Exists(vs2008))
                    {
                        System.IO.File.WriteAllText(vs2008 + "KeelKit.AddIn", str, System.Text.Encoding.Unicode);
                        WriteLog("2008:" + arg);
                    }
                    break;
                case "uninstallkeelkit":
                    WriteLog("卸载:" + arg);
                    if (System.IO.Directory.Exists(vs2005))
                    {
                        System.IO.File.Delete(vs2005 + "KeelKit.AddIn");
                    }
                    if (System.IO.Directory.Exists(vs2008))
                    {
                        System.IO.File.Delete(vs2008 + "KeelKit.AddIn");
                    }
                    break;
            }
        }

        private static void WriteLog(string s)
        {
            System.IO.File.AppendAllText("c:\\keelkit.log", s+Environment.NewLine );
        }
    }
}
